package com.wly.ui.gamescene
{
	import com.wly.box2d.BoxItem;
	import com.wly.interval.IntervalCallBack;
	import com.wly.ui.utils.CommonUtils;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	
	import Box2D.Common.Math.b2Vec2;
	
	public class Player extends MovieClip
	{
		private var _moveSpeed:int = 5;
		private var _deltaX:Number;
		private var _deltaY:Number;
		private var _moveAngle:Number;
		
		private var _moveInterval:IntervalCallBack;
		
		private var _boxItem:BoxItem;
		private var _gameScene:GameScene;
		
		public function Player()
		{
			super();
			
			mouseEnabled = false;
			mouseChildren = false;
		}
		
		public function setGameScene(val:GameScene):void
		{
			_gameScene = val;
		}
		
		public function startMove():void
		{
			stopMove();
			_moveInterval = new IntervalCallBack();
			_moveInterval.callbackFunc = movestep;
			_moveInterval.start();
		}
		
		public function stopMove():void
		{
			if(_moveInterval)
			{
				_moveInterval.stop();
				_moveInterval = null;
			}
		}
		
		private function movestep(param:*):void
		{
			this.x += _deltaX;
			this.y += _deltaY;
		}
		
		public function set moveAngle(val:Number):void
		{
			_moveAngle = val;
			_deltaX = _moveSpeed*Math.cos(_moveAngle);
			_deltaY = -_moveSpeed*Math.sin(_moveAngle);
		}
		
		public function set hitPostion(val:int):void
		{
			switch(val)
			{
				case CommonUtils.HIT_DOWN:
				case CommonUtils.HIT_UP:
					_deltaY = -_deltaY;
				 	break;
				case CommonUtils.HIT_LEFT:
				case CommonUtils.HIT_RIGHT:
					_deltaX = -_deltaX;
					break;
			}
		}
		
		public function changeMoveAngleByHitAngle(val:Number):void
		{
			var midAngle:Number = val+Math.PI/2;
			var newAngle:Number = midAngle+midAngle-_moveAngle;
			moveAngle = newAngle;
		}
		
		override public function set x(value:Number):void
		{
			super.x = value;
		}
		
		override public function set y(value:Number):void
		{
			//super.y = value;
			if(_gameScene)
			{
				_gameScene.onPlayerPosYUpdate(value);
			}
		}
		
		public function setBoxItem(boxItem:BoxItem):void
		{
			_boxItem = boxItem;
		}
		
		public function applyForcy(valX:Number, valY:Number):void
		{
			if(_boxItem && _boxItem.boxBody)
			{
				_boxItem.boxBody.ApplyForce(new b2Vec2(valX, valY), _boxItem.boxBody.GetWorldCenter());
			}
		}
		
		public function setTurePosY(val:Number):void
		{
			super.y = val;
		}
	}
}